<template>
	<view class="navbar">
		<view class="navbar-fixed">
			<!-- 状态栏 -->
			<!-- #ifdef -->
			<view :style="{height: statusBarHeight+'px'}"></view>
			<!-- #endif -->
			<!-- 导航栏内容 -->
			<view class="navbar-content" :class="{search: isSearch}" :style="{height: navbarHeight+'px', width: navbarWidth+'px'}" @click.stop="open">
				<view v-if="isSearch" class="navbar-content__search-icons">
					<uni-icons type="back" size="22" color="#fff" @click="back"></uni-icons>
				</view>
				<view v-if="!isSearch" class="navbar-search">
					<view class="navbar-search-icon">
						<uni-icons type="search" size="16" color="#999"></uni-icons>
					</view>
					<view class="navbar-search-text">
						Vue、react、uni-app
					</view>
				</view>
				<!-- 搜索页 -->
				<view v-else class="navbar-search">
					<input class="navbar-search-text" type="text" v-model="val" placeholder="请输入您要搜索的内容" @input="inputChange" />
				</view>
			</view>
		</view>
		<view :style="{height: statusBarHeight+navbarHeight +'px'}"></view>
	</view>
</template>

<script>
	export default {
		name:"navbar",
		props: {
			value: {
				type: [String, Number],
				default: ''
			},
			isSearch: {
				type: Boolean,
				default: false
			}
		},
		data() {
			return {
				statusBarHeight: 20,
				navbarHeight: 45,
				navbarWidth: 375,
				val: ''
			};
		},
		watch: {
			value(newVal) {
				this.val = newVal
			}
		},
		created() {
			// 获取手机系统信息
			const info = uni.getSystemInfoSync()
			this.statusBarHeight = info.statusBarHeight
			this.navbarWidth = info.windowWidth
			
			// 获取胶囊的位置
			// #ifndef H5 || APP-PLUS || MP-ALIPAY
			const menuButtonInfo = uni.getMenuButtonBoundingClientRect()
			// console.log(menuButtonInfo);
			// 导航栏高度，宽度
			this.navbarHeight = menuButtonInfo.height + 2 * (menuButtonInfo.top - info.statusBarHeight)
			this.navbarWidth = menuButtonInfo.left
			// #endif
			
			// #ifndef MP-ALIPAY
			info.statusBarHeight = 0 
			// #endif
		},
		methods: {
			back() {
				// uni.navigateBack()
				uni.switchTab({
					url: '/pages/tabbar/index/index'
				})
			},
			open() {
				if(this.isSearch) return
				uni.navigateTo({
					url: "/pages/home-search/home-search"
				})
			},
			inputChange(e) {
			  const	{ value } = e.detail
				this.$emit('input', value)
			}
		}
	}
</script>

<style lang="scss">
.navbar {
	.navbar-fixed {
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		z-index: 99;
		background-color: $mk-basic-color;
		.navbar-content {
			display: flex;
			justify-content: center;
			align-items: center;
			width: 100%;
			height: 45px;
			padding: 0 15px;
			box-sizing: border-box;
			.navbar-search {
				display: flex;
				align-items: center;
				width: 100%;
				height: 30px;
				padding: 0 10px;
				border-radius: 30px;
				background-color: #fff;
					.navbar-search-icon {
						margin-right: 10px;
					}
					.navbar-search-text {
						width: 100%;
						font-size: 14px;
						color: #999;
					}
			}
			&.search {
				padding-left: 0;
				.navbar-content__search-icons {
					margin: 0 10px;
				}
				.navbar-search {
					border-radius: 5px;
				}
			}
		}
		
	}
}
</style>
